﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class Content : BaseCtrl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.OnStart();
            }
        }

        /// <summary>
        /// 启动
        /// </summary>
        protected void OnStart()
        {
            this.BinderBigType();            
            this.BinderContent();
        }

      
        

        /// <summary>
        /// 绑定大类列表
        /// </summary>
        protected void BinderBigType()
        {
            rptBigList.DataSource = bllBigType.GetList();
            rptBigList.DataBind();
        }

        /// <summary>
        /// 绑定小类列表
        /// </summary>
        /// <param name="BigTypeID"></param>
        protected void BinderSmallType(int BigTypeID)
        {
            rptSmlList.DataSource = bllSmallType.GetList(BigTypeID);
            rptSmlList.DataBind();
        }

        /// <summary>
        /// 绑定大类所对应文章
        /// </summary>
        protected void BinderContent()
        {   
            int PageIndex = Pager.CurrentPageIndex;
            int PageSize = Pager.PageSize;
            int RecordCount = 0;
            rptContent.DataSource = bllContent.GetListByBigType(GetBigTypeID, PageIndex, PageSize, out RecordCount);
            rptContent.DataBind();
            Pager.RecordCount = RecordCount;
        }

        /// <summary>
        /// 绑定小类所对应文章
        /// </summary>
        protected void BinderSmallContent()
        {
            int PageIndex = Pager.CurrentPageIndex;
            int PageSize = Pager.PageSize;
            int RecordCount = 0;
            rptContent.DataSource = bllContent.GetListBySmallType(GetSmallTypeID , PageIndex, PageSize, out RecordCount);
            rptContent.DataBind();
            Pager.RecordCount = RecordCount;
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.BinderContent();
        }

        /// <summary>
        /// 删除，修改，操作
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptContent_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int PostID = Utils.StrToInt(e.CommandArgument.ToString(), 0);
            string Action = e.CommandName.ToLower().Trim();
            this.ItemContentOperate(PostID, Action);
        }

        

        /// <summary>
        /// 移动/更新/删除 大类
        /// </summary>
        /// <param name="BigTypeID"></param>
        /// <param name="Action"></param>
        protected void ItemContentOperate(int PostID, string Action)
        {
            switch (Action)
            {
                case "del":
                    OnDelete(PostID);
                    break;
                default:
                    break;
            }
            this.BinderContent();
        }


        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="PostID"></param>
        protected void OnDelete(int PostID)
        {
            bllContent.Delete(PostID.ToString());
        }
       

        #region "搜索"
        /// <summary>
        /// 全部文章
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnAll_Click(object sender, EventArgs e)
        {
            ViewState["BigTypeID"] = 0;
            ViewState["SmallTypeID"] = 0;
            //rptSmlList.Visible = false;
            BinderContent();            
        }

        protected void rptBigList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            if (e.CommandName.ToLower().Trim() == "viewbigtype")
            {
                int BigTypeID = Utils.StrToInt(e.CommandArgument.ToString(), 0);
                if (BigTypeID != 0)
                {
                    ViewState["BigTypeID"] = BigTypeID;
                    this.BinderSmallType(BigTypeID);
                   // rptSmlList.Visible = true;
                    BinderContent();
                }
            }
        }

        /// <summary>
        /// 绑定小类
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptSmlList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Trim() == "viewsmalltype")
            {
                int SmallTypeID = Utils.StrToInt(e.CommandArgument.ToString(), 0);
                if (SmallTypeID != 0)
                {
                    ViewState["SmallTypeID"] = SmallTypeID;
                    //this.BinderSmallType(SmallTypeID);                    
                    BinderSmallContent();
                }
            }
        }

        /// <summary>
        /// 获取大类ID
        /// </summary>
        protected int GetBigTypeID
        {
            get
            {
                if (ViewState["BigTypeID"] == null)
                    ViewState["BigTypeID"] = 0;
                return (int)ViewState["BigTypeID"];
            }
            set { ViewState["BigTypeID"] = value; }
        }

        /// <summary>
        /// 获取小类ID
        /// </summary>
        protected int GetSmallTypeID
        {
            get
            {
                if (ViewState["SmallTypeID"] == null)
                    ViewState["SmallTypeID"] = 0;
                return (int)ViewState["SmallTypeID"];
            }
            set { ViewState["SmallTypeID"] = value; }
        }

        #endregion


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string PostID = string.Empty;
            foreach (RepeaterItem Item in rptContent.Items)
            {
                Literal lbl = (Literal)Item.FindControl("lblPostID");
                CheckBox chk = (CheckBox)Item.FindControl("chkIndex");
                if (chk.Checked)
                {
                    PostID += lbl.Text + ",";
                }
            }
            PostID = PostID.TrimEnd(',');
            if (!string.IsNullOrEmpty(PostID))
            {
                bllContent.Delete(PostID);
                JScript.AlertAndRedirect("批量操作成功", "Content.aspx");
            }

        }

        /// <summary>
        /// 取消置顶
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUTop_Click(object sender, EventArgs e)
        {
            string PostID = string.Empty;
            foreach (RepeaterItem Item in rptContent.Items)
            {
                Literal lbl = (Literal)Item.FindControl("lblPostID");
                CheckBox chk = (CheckBox)Item.FindControl("chkIndex");
               
                if (chk.Checked)
                {
                    PostID += lbl.Text + ",";
                }  
            }
            PostID=PostID.TrimEnd(',');
            if (!string.IsNullOrEmpty(PostID))
            {
                bllContent.SetTopOnOff(PostID, true); //置顶
                JScript.AlertAndRedirect("批量操作成功", "Content.aspx");
            }
        }

        /// <summary>
        /// 取消置顶
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDTop_Click(object sender, EventArgs e)
        {
            string PostID = string.Empty;
            foreach (RepeaterItem Item in rptContent.Items)
            {
                Literal lbl = (Literal)Item.FindControl("lblPostID");
                CheckBox chk = (CheckBox)Item.FindControl("chkIndex");
                if (chk.Checked)
                {

                    if (chk.Checked)
                    {
                        PostID += lbl.Text + ",";
                    }
                }
            }
            PostID = PostID.TrimEnd(',');
            if (!string.IsNullOrEmpty(PostID))
            {
                bllContent.SetTopOnOff(PostID, false); //取消置顶
                JScript.AlertAndRedirect("批量操作成功", "Content.aspx");
            }
        }

        /// <summary>
        /// 批量移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMove_Click(object sender, EventArgs e)
        {
            string PostID = string.Empty;
            foreach (RepeaterItem Item in rptContent.Items)
            {
                Literal lbl = (Literal)Item.FindControl("lblPostID");
                CheckBox chk = (CheckBox)Item.FindControl("chkIndex");
                if (chk.Checked)
                {

                    if (chk.Checked)
                    {
                        PostID += lbl.Text + ",";
                    }
                }
            }
            PostID = PostID.TrimEnd(',');
            if (!string.IsNullOrEmpty(PostID))
            {
                int BigTypeID = YuRequest.GetFormInt(drplBigType.UniqueID, 0);
                int SmallTypeID = YuRequest.GetFormInt(drplSmallType.UniqueID, 0);
                if (BigTypeID == 0 || SmallTypeID == 0)
                {
                    return;
                }
                bllContent.MoveClass(PostID, BigTypeID, SmallTypeID); //取消置顶
                JScript.AlertAndRedirect("批量操作成功", "Content.aspx");
            }
        }

        protected string GetContent(object objString,int Length)
        {
            string str = objString.ToString().Trim();
            return Utils.GetUnicodeSubString(str, Length, "…");
        }

       

       
        
    }
}